/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Xiaobao Zeng<zengxiaobao@skyworth.com>
 * Created Time: 2016-12-16 18:08:11
 * Last Modified: 2017-08-01 19:49:47
 * File Name: qingke.go
 * Description:
 */

package qingke

import (
	"crypto/tls"
	"fmt"

	"doubimeizhi.com/utility"
	"github.com/astaxie/beego/httplib"
	log "github.com/cihub/seelog"
)

//添加洗衣机
func AddWasher(userId string, data interface{}) (bool, string, string) {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/washer/mgr/washerinfo/create/public", qkHost))
	nusb.SetURLParam("uid", userId)
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s//washer/mgr/washerinfo/create/public?appkey=271d8700b1074a9582a8cc4cdff94c5f&uid=%s", qkHost, userId)
	b := httplib.Post(reqURL)
	b.SetHost(qkHost)
	b.JSONBody(data)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-AddWasher, service page not found, url:%d", reqURL)
		return false, "", "server page not found"
	}
	log.Debugf("AddWasher-resp:%s", resp)
	if int(resp["code"].(float64)) == 0 {
		if washerId, ok := resp["washer_id"]; ok {
			return true, washerId.(string), "success"
		}
		return true, "", "success"
	}
	log.Warnf("rpc-qingke-AddWasher,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return false, "", resp["msg"].(string)
}

//修改洗衣机信息
func ModifyWasher(data interface{}) (int, string) {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/washer/mgr/washerinfo/modify", qkHost))
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/washer/mgr/washerinfo/modify?appkey=271d8700b1074a9582a8cc4cdff94c5f", qkHost)
	b := httplib.Post(reqURL)
	b.SetHost(qkHost)
	b.JSONBody(data)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-ModifyWasher, service page not found, url:%d", reqURL)
		return -1, "service page not found"
	}
	return int(resp["code"].(float64)), resp["msg"].(string)
}

//查询不同状态的洗衣机
func QueryKindWashers(userId string) (bool, interface{}) {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washer/statis", qkHost))
	nusb.SetURLParam("uid", userId)
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washer/statis?appkey=271d8700b1074a9582a8cc4cdff94c5f&uid=%s", qkHost, userId)
	b := httplib.Get(reqURL)
	b.SetHost(qkHost)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-QueryKindWashers, service page not found, url:%d", reqURL)
		return false, nil
	}
	if statis, ok := resp["data"]; ok {
		return true, statis
	}
	log.Warnf("rpc-qingke-QueryKindWashers,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return false, nil
}

//新版-查询不同状态的洗衣机，返回的结构不同
func NewQueryKindWashers(userId string) (bool, interface{}) {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washer/statis/new", qkHost))
	nusb.SetURLParam("uid", userId)
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washer/statis/new?appkey=271d8700b1074a9582a8cc4cdff94c5f&uid=%s", qkHost, userId)
	b := httplib.Get(reqURL)
	b.SetHost(qkHost)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-QueryKindWashers, service page not found, url:%d", reqURL)
		return false, nil
	}
	if statis, ok := resp["data"]; ok {
		return true, statis
	}
	log.Warnf("rpc-qingke-QueryKindWashers,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return false, nil
}

//根据洗衣机状态获取洗衣机信息
func QueryWahserInfoByStatus(userId string, status int) (bool, interface{}) {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washer/list/bystatus", qkHost))
	nusb.SetURLParam("uid", userId)
	nusb.SetURLParam("status", fmt.Sprintf("%d", status))
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washer/list/bystatus?appkey=271d8700b1074a9582a8cc4cdff94c5f&uid=%s&status=%d", qkHost, userId, status)
	b := httplib.Get(reqURL)
	b.SetHost(qkHost)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-QueryWahserInfoByStatus, service page not found, url:%d", reqURL)
		return false, nil
	}
	if washers, ok := resp["data"]; ok {
		return true, washers
	}
	log.Warnf("rpc-qingke-QueryWahserInfoByStatus,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return false, nil
}

//新版-根据洗衣机状态获取洗衣机信息
func NewQueryWahserInfoByStatus(userId string, status int) (bool, interface{}) {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washer/list/bystatus/new", qkHost))
	nusb.SetURLParam("status", fmt.Sprintf("%d", status))
	nusb.SetURLParam("uid", userId)
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washer/list/bystatus/new?appkey=271d8700b1074a9582a8cc4cdff94c5f&uid=%s&status=%d", qkHost, userId, status)
	b := httplib.Get(reqURL)
	b.SetHost(qkHost)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-NewQueryWahserInfoByStatus, service page not found, url:%d", reqURL)
		return false, nil
	}
	if washers, ok := resp["data"]; ok {
		return true, washers
	}
	log.Warnf("rpc-qingke-NewQueryWahserInfoByStatus,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return false, nil
}

//获取加盟商名下所有洗衣机信息
func GetAllWashers(userId string) interface{} {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washer/all", qkHost))
	nusb.SetURLParam("uid", userId)
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washer/all?appkey=271d8700b1074a9582a8cc4cdff94c5f&uid=%s", qkHost, userId)
	b := httplib.Get(reqURL)
	b.SetHost(qkHost)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-GetAllWashers, service page not found, url:%d", reqURL)
		return nil
	}
	if washers, ok := resp["data"]; ok {
		return washers
	}
	log.Warnf("rpc-qingke-GetAllWashers,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return nil
}

//新版-获取加盟商名下所有洗衣机信息
func NewGetAllWashers(userId string) interface{} {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washer/all/new", qkHost))
	nusb.SetURLParam("uid", userId)
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washer/all/new?appkey=271d8700b1074a9582a8cc4cdff94c5f&uid=%s", qkHost, userId)
	b := httplib.Get(reqURL)
	b.SetHost(qkHost)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-NewGetAllWashers, service page not found, url:%d", reqURL)
		return nil
	}
	if washers, ok := resp["data"]; ok {
		return washers
	}
	log.Warnf("rpc-qingke-NewGetAllWashers,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return nil
}

//根据洗衣房id获取所有洗衣机信息
func QueryWahserInfoByRoomid(userId, roomId string) (bool, interface{}) {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washer/list/byroom", qkHost))
	nusb.SetURLParam("uid", userId)
	nusb.SetURLParam("room_id", roomId)
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washer/list/byroom?appkey=271d8700b1074a9582a8cc4cdff94c5f&uid=%s&room_id=%s", qkHost, userId, roomId)
	b := httplib.Get(reqURL)
	b.SetHost(qkHost)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-QueryWahserInfoByRoomid, service page not found, url:%d", reqURL)
		return false, nil
	}
	if washers, ok := resp["data"]; ok {
		return true, washers
	}
	log.Warnf("rpc-qingke-QueryWahserInfoByRoomid,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return false, nil
}

//新版-根据洗衣房id获取所有洗衣机信息
func NewQueryWahserInfoByRoomid(userId, roomId string) (bool, interface{}) {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washer/qkl/list/byroom/new", qkHost))
	nusb.SetURLParam("uid", userId)
	nusb.SetURLParam("room_id", roomId)
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washer/qkl/list/byroom/new?appkey=271d8700b1074a9582a8cc4cdff94c5f&uid=%s&room_id=%s", qkHost, userId, roomId)
	b := httplib.Get(reqURL)
	b.SetHost(qkHost)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-NewQueryWahserInfoByRoomid, service page not found, url:%d", reqURL)
		return false, nil
	}
	if washers, ok := resp["data"]; ok {
		return true, washers
	}
	log.Warnf("rpc-qingke-NewQueryWahserInfoByRoomid,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return false, nil
}

//停启用洗衣机
func EnableWasher(userId string, data interface{}) interface{} {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washer/enable", qkHost))
	nusb.SetURLParam("uid", userId)
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washer/enable?appkey=271d8700b1074a9582a8cc4cdff94c5f&uid=%s", qkHost, userId)
	b := httplib.Post(reqURL)
	b.SetHost(qkHost)
	b.JSONBody(data)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-EnableWasher, service page not found, url:%d", reqURL)
		return nil
	}
	if runState, ok := resp["run_state"]; ok {
		return runState
	}
	log.Warnf("rpc-qingke-EnableWasher,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return nil
}

//查询用户洗衣机编号列表
func QueryWasheridList(userId string) interface{} {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/league/washer/list/washerid", qkHost))
	nusb.SetURLParam("uid", userId)
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/league/washer/list/washerid?appkey=271d8700b1074a9582a8cc4cdff94c5f&uid=%s", qkHost, userId)
	b := httplib.Get(reqURL)
	b.SetHost(qkHost)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-QueryWasheridList, service page not found, url:%d", reqURL)
		return nil
	}
	if washers, ok := resp["data"]; ok {
		return washers
	}
	log.Warnf("rpc-qingke-QueryWasheridList,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return nil
}

//定位洗衣机
func LocateWasher(washerId string) bool {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/cc/flash", qkHost))
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/cc/flash?appkey=271d8700b1074a9582a8cc4cdff94c5f", qkHost)
	b := httplib.Post(reqURL)
	b.SetHost(qkHost)
	b.JSONBody(map[string]interface{}{"washer_id": washerId})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-LocateWasher, service page not found, url:%d, err:%s", reqURL, err.Error())
		return false
	}
	if resp != nil {
		return true
	}
	log.Warnf("resp from qingke_washerLocate err")
	return false
}
func SwitchWasher(imei string) int {
	nusb := utility.NewURLSignBuilder("bbd5d840a15b4ff2953669004ade97bc", "a54df97469be435f8a28c5c123d15f64", fmt.Sprintf("https://%s/ccop/washer/switchcc", "qkcc.app.qkier.com"))
	reqURL := nusb.BuildURL()
	b := httplib.Post(reqURL)
	b.SetHost("qkcc.app.qkier.com")
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	data := map[string]interface{}{
		"imei":      imei,
		"switch_net": 0,
		"switch_addr": "",
	}
	b.JSONBody(data)
	var resp struct {
		Code int    `json:"code"`
		Msg  string `json:"msg"`
	}
	if err := b.ToJSON(&resp); err != nil {
		return 0
	}
	if resp.Code == 0 {
		return 1
	}
	return 0
}
//洗衣机运行信息查询
func ObtainWasherStateByWasherId(washerId string) (bool, interface{}) {
	nusb := utility.NewURLSignBuilder(qkAppkey, qkSecret, fmt.Sprintf("https://%s/washing/state/query", qkHost))
	nusb.SetURLParam("ver", "1")
	nusb.SetURLParam("washer_id", washerId)
	reqURL := nusb.BuildURL()
	//reqURL := fmt.Sprintf("https://%s/washing/state/query?appkey=271d8700b1074a9582a8cc4cdff94c5f&ver=1&washer_id=%s", qkHost, washerId)
	b := httplib.Get(reqURL)
	b.SetHost(qkHost)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-qingke-ObtainWasherStateByWasherId, service page not found, url:%d, err:%s", reqURL, err.Error())
		return false, nil
	}
	if int(resp["code"].(float64)) == 0 {
		return true, resp["run_state"]
	}
	log.Warnf("resp from ObtainWasherStateByWasherId err, washer_id:%s, code:%v, msg:%v", washerId, resp["code"], resp["msg"])
	return false, nil
}
